package com.lashou.chunshen.lashoutuangou;

import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.lashou.chunshen.lashoutuangou.core.BaseActivity;
import com.lashou.chunshen.lashoutuangou.core.BaseFragment;
import com.lashou.chunshen.lashoutuangou.fragments.HomeFragment;
import com.lashou.chunshen.lashoutuangou.fragments.HomeFragment01;
import com.lashou.chunshen.lashoutuangou.fragments.LoadingFragment;
import com.lashou.chunshen.lashoutuangou.fragments.ShareSdkSMSFragment;

import butterknife.InjectView;

public class MainActivity extends BaseActivity {
    @InjectView(R.id.tabhost)
    FragmentTabHost tabhost;
    LayoutInflater layoutInflater;

    //数组存放Fragment界面
    private Class fragmentArray[] = {HomeFragment.class, HomeFragment01.class, LoadingFragment.class, ShareSdkSMSFragment.class};
    //按钮图片
    private int mImageViewArray[] = {R.drawable.tab_home_btn, R.drawable.tab_albums_btn, R.drawable.tab_songs_btn, R.drawable.tab_more_btn};
    //Tab文字
    private String mTextViewArray[] = {"首页", "周边", "我的", "更多"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }


    private void initView() {
        layoutInflater = LayoutInflater.from(this);
        //实例化TabHost对象，得到TabHost
        tabhost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        tabhost.getTabWidget().setDividerDrawable(R.color.white);//分割线去除

        //得到fragment的个数
        int count = fragmentArray.length;
        for (int i = 0; i < count; i++) {
            //为每一个Tab按钮设置图标、文字和内容
            TabHost.TabSpec tabSpec = tabhost.newTabSpec(mTextViewArray[i]).setIndicator(getTabItemView(i));
            //将Tab按钮添加进Tab选项卡中
            tabhost.addTab(tabSpec, fragmentArray[i], null);
            //设置Tab按钮的背景
            tabhost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.selector_tab_background);
        }
    }

    /**
     * 给Tab按钮设置图标和文字
     */
    private View getTabItemView(int index) {
        View view = layoutInflater.inflate(R.layout.tab_item_view, null);

        ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
        imageView.setImageResource(mImageViewArray[index]);

        TextView textView = (TextView) view.findViewById(R.id.textview);
        textView.setText(mTextViewArray[index]);

        return view;
    }

    @Override
    protected BaseFragment getFirstFragment() {
        return null;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_main;
    }

    @Override
    protected int getFragmentContentId() {
        return R.layout.activity_main;
    }

    @Override
    protected boolean isNeedTitleBar() {
        return false;
    }
}
